package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.utils.BaiduAuditUtils;
import cn.itsource.org.domain.Employee;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.domain.ShopAuditLog;
import cn.itsource.org.mapper.EmployeeMapper;
import cn.itsource.org.mapper.ShopAuditLogMapper;
import cn.itsource.org.mapper.ShopMapper;
import cn.itsource.org.service.IShopService;
import cn.itsource.basic.basic.service.impl.IBaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-08-05
 */
@Service
public class ShopServiceImpl extends IBaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;
    public void  send(Integer integer){

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        //设置发送人
        mailMessage.setFrom("1831595042@qq.com");
        if(integer == 2){
            //邮件主题
            mailMessage.setSubject("审核通过");
            //邮件内容
            mailMessage.setText("success");
        }else {
            //邮件主题
            mailMessage.setSubject("审核驳回");
            //邮件内容
            mailMessage.setText("error");
        }
        //收件人
        //mailMessage.setTo("1508019444@qq.com");
        mailMessage.setTo("1831595042@qq.com");
        javaMailSender.send(mailMessage);
        System.out.println("发送成功");
    }

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //1.校验空参数
        if(StringUtils.isEmpty(shop.getName())){
            throw new CustomException("店铺名称不可为空！");
        }
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new CustomException("店铺名称存在违规字符！");
        }
        //2.校验店铺名称是否已经存在
        Shop shop1 = shopMapper.selectByShopName(shop.getName());
        if(shop1 != null){
            throw new CustomException("店铺名称已存在!");
        }
        //3.校验员工是否存在
        Employee employee=employeeMapper.selectByUserName(shop.getAdmin().getUsername());
        if(employee != null){
            throw new CustomException("员工已注册，请确认信息!");
        }
        //4.保存员工
        employeeMapper.insert(shop.getAdmin());
        //5.保存店铺
        System.out.println(shop);
        shopMapper.insert(shop);
    }

    @Override
    public void auditPass(ShopAuditLog shopAuditLog) {
        //保存审核日志
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        System.out.println(shop);
        if(shop == null){
            throw new CustomException("数据异常");
        }
        shop.setState(2);
        shopMapper.update(shop);
        //发邮件
        send(2);
    }

    @Override
    public void auditReject(ShopAuditLog shopAuditLog) {
        //保存审核日志
        shopAuditLogMapper.insert(shopAuditLog);
        //修改店铺状态
        Shop shop = shopMapper.selectById(shopAuditLog.getShopId());
        System.out.println(shop);
        if(shop == null){
            throw new CustomException("数据异常");
        }
        shop.setState(4);
        shopMapper.update(shop);
        //发邮件
        send(4);
    }

    @Override
    public List<Long> statistics() {
        return shopMapper.statistics();
    }

    @Override
    public void addBatch(List<Shop> shop) {
        shopMapper.addBatch(shop);
    }

}
